﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Share/_Layout.cshtml";
}

@section head{
        
        <div class="col-sm-4">
            <ol class="breadcrumb">
                <li>
                    <a href="/">Home</a>
                </li>
                <li>
                    <a href="/">Tools</a>
                </li>
                <li class="active">
                    <strong>代码生成器</strong>
                </li>
            </ol>
        </div>

}


<div class="row">
    <div class="col-lg-2">
        <div class="panel panel-default">
            <div class="panel-heading"><strong>Tables</strong></div>
            <div id="tables-group" class="list-group"></div>
        </div>
    </div>
    <div class="col-lg-10">
        <div class="col-lg-4">
            <div class="panel">
                <input id="txtColumnDesc" type="text" class="form-control" placeholder="Column Desc...">
            </div>
        </div>
        <div class="col-lg-4">
                <div class="input-group" id="btnBuilder">
                    <input id="txtColumnName" type="text" class="form-control" placeholder="Column Name...">
                    <div class="input-group-btn">
                        <button data-toggle="dropdown" class="btn btn-primary dropdown-toggle" type="button">Generate <span class="caret"></span></button>
                        <ul class="dropdown-menu pull-right">
                            <li><a href="#">string</a></li>
                            <li><a href="#">int</a></li>
                            <li><a href="#">DateTime</a></li>
                            <li class="divider"></li>
                            <li><a href="#">bool</a></li>
                            <li><a href="#">decimal</a></li>
                            <li><a href="#">long</a></li>
                        </ul>
                    </div>
                </div>
        </div>
        <div class="col-lg-4">
        </div>


        <div class="col-lg-12">
            <div class="panel panel-success">
                <div class="panel-heading"><strong>Model</strong></div>
                <pre id="model"></pre>
            </div>
        </div>
    </div>
</div>
<!-- /.row -->



@section scripts{

    <script type="text/javascript">
        $(function () {
            InitControlEvent();
        });
        
        function GenerateColumnModel(col, desc, type) {
            var defaultValue = '', defaultValue2 = '', dataType = '';
            switch (type) {
                case 'int':
                    defaultValue = 'AppConstCommon.IntNull';
                    defaultValue2 = defaultValue;
                    dataType = 'DbType.Int32';
                    break;
                case 'long':
                    defaultValue = 'AppConstCommon.IntNull';
                    defaultValue2 = defaultValue;
                    dataType = 'DbType.Int64';
                    break;
                case 'string':
                    defaultValue = 'AppConstCommon.StringNull';
                    defaultValue2 = defaultValue;
                    dataType = 'DbType.String';
                    break;
                case 'DateTime':
                    defaultValue = 'AppConstCommon.DateTimeNull';
                    defaultValue2 = 'null';
                    dataType = 'DbType.DateTime';
                    break;
                case 'bool':
                    defaultValue = 'false';
                    defaultValue2 = defaultValue;
                    dataType = 'DbType.Boolean';
                    break;
                case 'decimal':
                    defaultValue = 'AppConstCommon.DecimalNull';
                    defaultValue2 = defaultValue;
                    dataType = 'DbType.Decimal';
                    break;
            }

            var a = '';
            a += WriteLine('private ' + type + ' m_' + col + ' = ' + defaultValue + ';', 2);
            a += WriteLine('/// &lt;summary&gt;', 2);
            a += WriteLine('/// 获取或设置 ' + col + ' 属性值,描述:' + desc, 2);
            a += WriteLine('/// &lt;/summary&gt;', 2);
            a += WriteLine('[DataAttribute(fieldName: "' + col + '", dataType: ' + dataType + ', isPrimaryKey: false, isIdentity: false, ', 2);
            a += WriteLine('isAllowNull: true, needInsert: true, needUpdate: true, defaultValue: ' + defaultValue2 + ')]', 2);
            a += WriteLine('[DataMember]', 2);
            a += WriteLine('[Description("' + desc + '")]', 2);
            a += WriteLine('public ' + type + ' ' + col, 2);
            a += WriteLine('{', 2);
            a += WriteLine('get { return m_' + col + '; }', 3);
            a += WriteLine('set { m_' + col + ' = value; }', 3);
            a += WriteLine('}', 2);
            $('#model').html(a);
        }



        function InitControlEvent() {
            $('#btnBuilder').click(function (e) {
                BubbleEvent(e, 'A', function (tag) {
                    var columnName = $('#txtColumnName').val();
                    var columnDesc = $('#txtColumnDesc').val();
                    GenerateColumnModel(columnName, columnDesc, $(tag).text());
                });
            });
        }
        function BubbleEvent(e, tagName, callback) {
            e = e || window.event;
            var target = e.target || e.srcElement;
            if (target.nodeName.toString().toUpperCase() == tagName.toUpperCase()) {
                callback(target);
            }
        }
        function WriteLine(code, type) {
            var nbsp = '&nbsp;&nbsp;&nbsp;&nbsp;';
            var a = '';
            for (var i = 0; i < type; i++) {
                a += nbsp;
            }
            a += code + '<br/>';
            return a;
        }
    </script>

}
